package com.haiyou.dc.api.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyou.common.util.R;
import com.haiyou.common.util.ValidatorUtil;
import com.haiyou.dc.api.entity.Movie;
import com.haiyou.dc.api.mapper.MovieMapper;
import com.haiyou.dc.api.service.MovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author root
 * @date 2020-01-04 18:37:46
 */
@RestController
@RequestMapping("/movie")
@Slf4j
@Api(tags = "电影")
@Validated
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieMapper movieMapper;

    @ApiOperation(value = "新增辅导类型", notes = "新增辅导类型")
    @PostMapping(value = "addCompostionTyp1e")
    public R addCompostionTyp1e(@ApiParam(value = "成绩") @NotNull(message = "成绩不能为空") @Max(value = 60, message = "得分必须小于60") @RequestParam(required = false) Integer score
            , @ApiParam(value = "年龄") @NotNull(message = "年龄不能为空") @Min(value = 3, message = "年龄大于3岁") @RequestParam(required = false) Integer age
            , @ApiParam(value = "辅导作文类型") @NotNull(message = "辅导作文类型不能为空") @RequestParam(required = false) String name) {
        //校验
        log.info(String.valueOf(age));
        return R.ok();
    }


    @ApiOperation(value = "保存数据")
    @PutMapping("/save")
    public R save(@RequestBody Movie movie) {
        movieService.insertSelective(movie);
        return R.ok();
    }

    @ApiOperation(value = "查看数据")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        Movie movie = movieService.selectByPrimaryKey(id);
        return R.ok(movie);
    }

    @ApiOperation(value = "修改数据")
    @PostMapping("/update")
    public R update(@RequestBody Movie movie) {
        ValidatorUtil.validateEntity(movie);
        movieService.updateByPrimaryKeySelective(movie);
        return R.ok();
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        movieService.deleteByPrimaryKey(id);
        return R.ok();
    }

    @ApiOperation(value = "查看列表数据")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        Example example = new Example(Movie.class);
        example.orderBy("id").desc();
        Page<Movie> page = PageHelper.startPage(pageNum, pageSize, true, false, false);
        movieService.selectByExample(example);
        PageInfo<Movie> info = new PageInfo<>(page.getResult());
        return R.ok(info);
    }

    @ApiOperation(value = "测试mybatis")
    @GetMapping("/testInfo")
    public R testInfo() {
        List<Integer> ids= new ArrayList<>();
        ids.addAll(Arrays.asList(24,82,843,393,146));
        String condition=" movie_title like '%龙%' ";
        List<Movie> movieList = movieMapper.selectList(ids, condition);
        return R.ok(movieList);
    }

}